<?php

/**
 * @name  活动报名管理
 * @author wangbin
 * @email 289807170@qq.com
 * @data 2015-11-26  18:38:44
 */
//访问权限标识
define(C("ACCESSACTION"), true);
import('jinghuan.Hxapi');

class ActiveenlistAction extends CommonAction {

    function _initialize() {
        //    $this->auth_allow_function[] = 'get_image';
    }

    function index() {
        if ($this->isAjax()) {
            $this->active_type = D(TABLE_SOURCE_BASIC_FIELD)->field("concat(group_id,id) as id,name")->where("group_id=1007")->select();
            $this->callback($this->fetch());
        }
    }

    function datatable() {
        if ($this->isAjax()) {
            $post = post_datatable();
            foreach ($post['sql_where'] as $f => $v) {
                if (!empty($v)) {
                    if ($f === 'active_type_id') {
                        $post["sql_where"][$f] = array('EQ', $v);
                    } else {
                        $post["sql_where"][$f] = array('exp', "like '%" . $v . "%'");
                    }
                }
            }
            $voList = V("Activeenlist")
                    ->where($post["sql_where"])
                    ->order('status desc,u_time desc')
                    ->limit($post["sql_limit"])
                    ->select();
//               $this->func("$.Log", print_r(D()->getLastSql(), TRUE));
            $count = V("Activeenlist")
                    ->where($post["sql_where"])
                    ->order('status desc,u_time desc')
                    ->count();
            $this->callback(datatable($voList, $post["sEcho"], $count));
        }
    }

    function edit() {
        if ($this->isAjax()) {
            $this->id = $id = $this->_q("id");
            //      $this->func("$.Log",  print_r($id,TRUE));
            $this->data = D(TABLE_ACTIVE_ACTIVE_ENLIST)->where(array("id" => $id))->find();
            $this->sign_status = $s = D(TABLE_SOURCE_BASIC_FIELD)->field("concat(group_id,id) as id, name")->where(array("group_id" => 1010))->select();
            $this->callback($this->fetch());
        }
    }

    function do_edit() {
        if ($this->isAjax()) {
            $data = $this->_q();
            //$this->func("$.Log", print_r($data, TRUE));
            $xdata = D(TABLE_ACTIVE_ACTIVE_ENLIST)
                    ->where(array("id" => $data["id"]))
                    ->save(array(
                "sign_status_id" => $data["sign_status_id"],
                "content" => $data["data"],
                "u_time" => time()
            ));
            $enlist = D(TABLE_ACTIVE_ACTIVE_ENLIST)->where(array("id" => $data["id"]))->field('active_name_id,client_log_id')->find();
            $groupid = D(TABLE_ACTIVE_ACTIVE_NAME)->where('id=%s', $enlist['active_name_id'])->field('title,groupid')->find();
            $account = D(TABLE_SOURCE_CLIENT_LOGIN)->where('id=%s', $enlist['client_log_id'])->field('account,nickname')->find();
            if ($groupid['groupid'] && $account['account']) {
                $hx = new Hxapi();
                if ($data["sign_status_id"] == '10101075' && $xdata !== false) {
                    $data_['group_id'] = $groupid['groupid'];
                    $data_['username'] = $account['account'];
                    $res = $hx->chatgroups_add_user($data_);
                    if ($res == 1) {
                        //环信发消息
                        $data['target_type'] = 'chatgroups';
                        $data['target'][] = $groupid['groupid'];
                        $data['msg']['type'] = 'txt';
                        $data['msg']['msg'] = '欢迎' . $account['nickname'] . '参加' . $groupid['title'];
                        $data['from'] = '10000';
                        $data['ext']['imageAttribute'] = C('HOST') . "/logo.png";
                        $data['ext']['nickAttribute'] = '50+团队';
                        $data['ext']['msgtype'] = '0';
                        $hx->sendmessage($data);
                    }
                } else {
                    $data1_['group_id'] = $groupid['groupid'];
                    $data1_['username'] = $account['account'];
                    $hx->chatgroups_del_user($data1_);
                }
            }
            $this->callback();
        }
    }

}
